import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useAppStore = defineStore('app', () => {
  // 应用状态
  const loading = ref(false)
  const title = ref('校园生活圈')
  const theme = ref<'light' | 'dark'>('light')
  
  // 设置加载状态
  const setLoading = (status: boolean) => {
    loading.value = status
  }
  
  // 设置页面标题
  const setTitle = (newTitle: string) => {
    title.value = newTitle
    document.title = `${newTitle} - 校园生活圈`
  }
  
  // 切换主题
  const toggleTheme = () => {
    theme.value = theme.value === 'light' ? 'dark' : 'light'
    localStorage.setItem('theme', theme.value)
  }
  
  // 初始化主题
  const initTheme = () => {
    const savedTheme = localStorage.getItem('theme') as 'light' | 'dark'
    if (savedTheme) {
      theme.value = savedTheme
    }
  }
  
  // 初始化
  initTheme()
  
  return {
    loading,
    title,
    theme,
    setLoading,
    setTitle,
    toggleTheme,
    initTheme
  }
})